const getHashValGenerator = () => {
	const hashValMap = new Map();

	return function getHashVal(hash) {
		if (hashValMap.has(hash)) {
			return hashValMap.get(hash);
		} else {
			const hash_val = hashValMap.size + 1;
			hashValMap.set(hash, hash_val);
			return hash_val;
		}
	};
};
const getNumberVal = getHashValGenerator();
const getTypeVal = getHashValGenerator();

const CARD_INSTANCE_CACHE = new Map();
class Card {
	static getIns(txt) {
		return CARD_INSTANCE_CACHE.get(txt);
	}
	static buildIns(type, number) {
		const txt = `${type}_${number}`;
		if (CARD_INSTANCE_CACHE.has(txt)) {
			return CARD_INSTANCE_CACHE.get(txt);
		} else {
			const ins = new Card(type, number);
			CARD_INSTANCE_CACHE.set(txt, ins);
			return ins;
		}
	}
	constructor(type, number) {
		this.type = type;
		this.number = number;
		this._type_val = getTypeVal(type);
		this._val = getNumberVal(number);
		// 用于分数对比时的权重值
		this.weight = this._val === 1 ? 14 : this._val;
		this._txt = type + "_" + number;
	}
	valueOfType() {
		return this._type_val;
	}
	valueOf() {
		return this._val;
	}
	toString() {
		return this._txt;
	}
	toJSON() {
		return this._txt;
	}
}
module.exports = Card;

class CardList extends Array {
	static ToCardList(arr) {
		var res = arr;
		if (!(res instanceof CardList)) {
			res = new CardList(...arr);
		}
		return res;
	}
	static sort_cards(list) {
		return list.sort((a, b) => a - b);
	}
	static filter_remove_cards(list, remove_list) {
		const remove_map = new WeakMap();
		remove_list.forEach(card => remove_map.set(card, true));
		return list.filter(card => !remove_map.has(card));
	}
	first() {
		return this[0];
	}
	last() {
		return this[this.length - 1];
	}
	sort_cards() {
		return CardList.sort_cards(this);
	}
	// toString() {
	// 	return "[" + this.map(card => String(card)).join(",") + "]"
	// }
	// toJSON() {
	// 	return this.toString();
	// }
}
Card.CardList = CardList;
